/**
* Created by yyr on 2018/3/16
*已投资基金详情选项卡基金分配
*/
<template>
    <div>
        <div class="ca-box-border-top-none ca-p-22">
            <div class="ca-title">
                <h3>基金分配</h3>
                <div class="ca-pull-right ca-p-b-20">
                    <el-button  type="primary" class="ca-m-l-10" @click="distributionTotal">分配总计</el-button>
                    <el-button  type="primary" class="ca-m-l-10"
                                :disabled="!isAuthDistributionEdit"
                                @click="addDistribution">
                                添加</el-button>
                </div>
            </div>
            <div class="ca-box ca-p-22" v-for="(row,index) in distributionList"  :key="index" :class="[{'ca-m-t-30':index>0}]">
                <div class="ca-title" v-if="!distributionList[index].editStatus">
                    <h3>分配信息</h3>
                    <div class="ca-pull-left ca-m-l-20" style="line-height:36px;font-size:14px;color:#666">
                        {{$moment( row.disTime ).format("YYYY-MM-DD")}}
                    </div>
                    <div class="ca-pull-right" :span="4">
                        <button type="button" @click="fundAllocationOpen(index)"  v-if='row.show' class="ca-btn-switch ca-btn-default ca-m-l-10"><i class="el-icon-arrow-down fa-lg ca-m-r-5"></i><span>展开</span></button>
                        <button type="button" @click="fundAllocationOpen(index)" class="ca-btn-switch ca-btn-default ca-m-l-10" v-else ><i class="el-icon-arrow-up ca-m-r-5 fa-lg"></i><span>收起</span></button>
                        <el-button type="text" @click="fundDistributionEdit(row,index)" :disabled="!isAuthDistributionEdit" class="ca-edit-btn el-icon-edit ca-m-l-20">编辑</el-button>
                    </div>
                </div>
                <div v-if="row.hide">
                    <el-row class="ca-m-t-10">
                        <el-col :span="8" class="ca-font-size-14 wordWrap">
                            <span class="ca-color__titleColor ca-font-size-14">分配次数：</span>第{{row.frequency}}次
                        </el-col>
                        <el-col :span="8" class="ca-font-size-14">
                            <span class="ca-color__titleColor ca-font-size-14">分配总金额(万元)：</span><span class="ca-color__money ca-font-size-14">{{row.money | format1}} 万元</span>
                        </el-col>
                        <el-col :span="8" class="ca-font-size-14" v-if="!isMotherFund">
                            <span class="ca-color__titleColor ca-font-size-14">分配项目：</span>
                            <span class="ca-color__money ca-font-size-14 ca-m-r-10" v-for="item in row.itemList" :key="item.id">
                                {{item.itemName}}
                            </span>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20" class="ca-m-t-10">
                        <el-col :span="24" class="ca-font-size-14">
                            <span class="ca-color__titleColor ca-font-size-14">分配原因：</span>{{row.reason}}
                        </el-col>
                    </el-row>
                    <el-table
                        :data="row.historyList"
                        style="width:100%" class="table_p_01 ca-m-t-15 ca-table">
                        <el-table-column
                            prop="lpType"
                            show-overflow-tooltip
                            label="合伙人类型">
                            <template slot-scope="scope">
                                {{scope.row.lpType | csConstFilter('PARTNER_TYPE')}}
                            </template>
                        </el-table-column>
                        <el-table-column
                            show-overflow-tooltip
                            prop="lpName"
                            label="合伙人名称">
                            <template slot-scope="scope">
                                <el-button
                                    type="text"
                                    @click="goPartnerDetail(scope.row.lpId,scope.row.partnerType)"
                                    :disabled="scope.row.existence">{{scope.row.lpName}}
                                </el-button>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="interests"
                            label="权益(%)">
                        </el-table-column>
                        <el-table-column
                            prop="amount"
                            label="分配金额(万元)">
                            <template slot-scope="scope">
                                {{scope.row.amount | format1}}
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-row class="ca-m-t-15">
                        <div class="ca-title"><h3>附件</h3></div>
                    </el-row>
                    <div class="ca-file-ul">
                        <el-row class="ca-file-row" :gutter="20" v-for="(file,fileIndex) in row.fileList" :key="fileIndex">
                            <el-col :span="4" class="upload-time">{{file.uploadTime|moment('YYYY-MM-DD')}}</el-col>
                            <el-col :span="1">
                                <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                            </el-col>
                            <el-col :span="8">
                                <div class="ca-text-over-flow-ellipsis ca-pointer"
                                     @click="previewFileCheck(file.id, file.fileName)">
                                    <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                                </div>
                            </el-col>
                            <el-col :span="4">
                                <div class="ca-text-over-flow-ellipsis">{{userName}}</div>
                            </el-col>
                            <el-col :span="3" class="ca-pull-right ca-align-right">
                                <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                            </el-col>
                        </el-row>
                        <div v-if="!fileDetail" class="ca-font-color-999">暂无数据</div>
                    </div>
                </div>
                <csDistributionEdit
                    :editData="editData"
                    :editIndex="editIndex"
                    @cancelEdit="cancelEdit"
                    @changeData="changeData"
                    @changeInterests='changeInterests'
                    v-if="distributionList[index].editStatus"
                    @addRedDot="addRedDot"
                    :isMotherFund="isMotherFund"
                ></csDistributionEdit>
            </div>
        </div>
        <el-dialog :visible.sync="dialogTotal"  title="基金分配总计">
            <el-row class="ca-title"><h3>分配总金额(万元)：</h3></el-row>
            <el-table style="width:100%" class="ca-table ca-m-t-15" :data="totalData">
                <el-table-column
                    prop="guestName"
                    label="合伙人名称">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="goPartnerDetail(scope.row.id,scope.row.partnerType)"
                            :disabled="scope.row.existence">{{scope.row.guestName}}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="money"
                    label="分配金额(万元)">
                    <template slot-scope="scope">
                        {{scope.row.money | format1}}
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
        <el-dialog :visible.sync="dialogFormVisible"  width='70%' :show-close="false">
            <el-form label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="分配时间">
                            <el-date-picker type="date" 
                                v-model="distributionDate.disTime" 
                                placeholder="选择日期" 
                                @change="changeDate"
                                style="width:200px"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" class="ca-align-right">
                        <el-button type="primary" @click="addDistributionData()">保 存</el-button>
                        <el-button @click="cancelDialog">取 消</el-button>
                    </el-col>
                </el-row>
                <el-form-item label="分配次数">
                    <el-input type="number" class="ca-vertical-align-middle" v-model="distributionDate.frequency" placeholder="选择输入内容" style="width:200px"></el-input>
                </el-form-item>
                <el-row v-if="!isMotherFund">
                    <el-col :span="8">
                        <el-form-item label="分配项目">
                            <el-select v-model="addItemList"
                                       style="width:90%;"
                                       filterable placeholder="请选择"
                                       @change="addChangeItem">
                                <el-option
                                    v-for="item in itemListAll"
                                    :key="item.id"
                                    :label="item.itemName"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="15" class="ca-m-l-20">
                        <el-tag
                            v-for="item in distributionDate.itemList"
                            :key="item.id"
                            :closable="true"
                            :close-transition="false"
                            @close="closeItem(item)"
                            :type="item.type" class="ca-m-r-10 ca-m-t-8">
                            {{item.itemName}}
                        </el-tag>
                    </el-col>
                </el-row>
                <el-form-item label="分配原因" class="ca-m-t-10">
                    <el-input type="textarea" :maxlength="60" v-model="distributionDate.reason" placeholder="选择输入内容"></el-input>
                </el-form-item>
            </el-form>
            <table class="ca-table ca-m-t-15">
                <tr>
                    <th width="30%">合伙人名称</th>
                    <th width="30%">权益占比(%)</th>
                    <th width="30%">分配金额(万元)</th>
                    <th width="10%" class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="pushDistribution(-1)"><i class="fa fa-plus"></i></button></th>
                </tr>
                <tr v-for="(row,index) in distributionDate.historyList" :key="index">
                    <td>
                        <el-select v-model="row.lpId"
                                   @change="addChangePartner(index,row)"
                                   filterable placeholder="请选择" style="width:100%;">
                            <el-option
                                v-for="item in partnerAllList"
                                :key="item.lpId"
                                :label="item.lpName"
                                :value="item.lpId">
                            </el-option>
                        </el-select>
                    </td>
                    <td>
                        <el-input v-model="row.interests" class="ca-vertical-align-middle" placeholder="选择输入内容" disabled></el-input>
                    </td>
                    <td>
                        <el-input v-model="row.amount" type="number" class="ca-vertical-align-middle" placeholder="选择输入内容"></el-input>
                    </td>
                    <td class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="deleteDistributionSingle(row,index,-1)"><i class="fa fa-remove"></i></button></td>
                </tr>
            </table>
            <el-row class="ca-title ca-m-15">
                <h3>附件</h3>
                <div class="ca-align-right">
                    <csUpload
                        :fileType="fileType"
                        :businessId="itemId"
                        uploadType="all"
                        :getFileUploadedReturnData="getFileUploadedReturnDataFun"
                    ></csUpload>
                </div>
            </el-row>
            <div class="ca-file-ul">
                <el-row class="ca-file-row" :gutter="20" v-for="(file, fileIndex) in fileDetail" :key="fileIndex">
                    <el-col :span="4" class="upload-time">{{file.uploadTime|moment('YYYY-MM-DD')}}</el-col>
                    <el-col :span="1">
                        <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                    </el-col>
                    <el-col :span="8">
                        <div class="ca-text-over-flow-ellipsis ca-pointer"
                             @click="previewFileCheck(file.id, file.fileName)">
                            <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                        </div>
                    </el-col>
                    <el-col :span="4">
                        <div class="ca-text-over-flow-ellipsis">{{userName}}</div>
                    </el-col>
                    <el-col :span="3" class="ca-pull-right ca-align-right">
                        <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                        <el-button class="ca-table-btn" @click="deleteFileItem(fileIndex)">
                            <i class="fa fa-times"></i>
                        </el-button>
                    </el-col>
                </el-row>
                <div v-if="!fileDetail" class="ca-font-color-999">暂无数据</div>
            </div>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from '../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import {isNoAuth} from '__service/utils';
    import mined from '__components/file-list/mixin';
    export default {
        mixins: [mined],
        data() {
            return {
                isAuthDistributionEdit: true,
                dialogFormVisible: false,
                dialogTotal: false,
                itemId: this.$route.params.fundId,
                distributionDate: {  //添加时候
                    disTime: '',//分配时间
                    frequency: '',//分配次数
                    fundId: '',
                    id: '',
                    historyList: [],
                    itemList: [],//分配项目
                    money: '',//分配金额
                    reason: '',//分配原因
                    fileList: [],
                },
                historyListItem: {//点击加号添加一行
                    amount: '',  //分配金额
                    attribute: '',//资金属性
                    distributionId: '',//分配表主id ,
                    id: '',
                    interests: '',//权益
                    lpId: '',//合伙人id
                    lpName: '',//合伙人名称
                    lpType: '',//合伙人类型
                    partnerType: '1',//合伙人类型：1：客户,2:母基金
                },
                itemList: [{
                    disId: '',//分配id
                    id: '',
                    itemId: '',//项目id
                    itemName: '',//项目名称
                }],
                distributionList: [],//获取的列表
                itemListAll: [],//所有的项目
                addItemList: '',//添加选择项目
                partnerAllList: [],//所有的合伙人
                fileType: '',
                editData: {},
                editIndex: 0,
                fileDetail: [],//上传成功文件返回的数据
                totalData: [],
                userName: this.$store.state.user.cnName,
            }
        },
        props: {
            fundId: {
                type: String,
                default() {
                    return ''
                }
            },
            isMotherFund: {
                type: Boolean,
                default() {
                    return false
                }
            },
            isAuth: {
                type: String,
                default() {
                    return '';
                }
            }
        },
        watch: {

        },
        created() {
            if (this.isMotherFund === true) {
                this.itemId = this.fundId;
            }
            this.distributionDate.fundId = this.itemId;
            this.getDistributionList();
            this.init();
            // this.getPartnerInfo();
            this.isAuthDistributionEdit = this.isAuth ? isNoAuth(this.isAuth) : isNoAuth('已投资基金-基金分配-编辑');
        },
        mounted() {
            let fileTypeList = this.$store.state.user.fileFloderDicVoList;
            fileTypeList.map(item=>{
                if (item.name === '基金分配') {
                    this.fileType = item.type;
                }
            })
        },
        methods: {
            init() {
                apiHttp(api.GET_PROJECT).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemListAll = ret.data;
                    }
                })
            },
            //添加基金分配弹出框
            addDistribution() {
                this.dialogFormVisible = true;
                this.addItemList = '';
                this.changeDate(new Date()); //获取最新日期下的合伙人
            },
            //添加  文件成功返回的数据
            getFileUploadedReturnDataFun(arr) {
                arr.map(item=>{
                    this.distributionDate.fileList.push(item.id)
                })
                this.fileDetail = this.fileDetail.concat(arr);
            },
            //删除添加的一条文件
            deleteFileItem(index) {
                this.distributionDate.fileList.splice(index, 1);
                this.fileDetail.splice(index, 1);
            },
            //获取分配列表
            getDistributionList() {
                apiHttp(api.GET_FUND_DISTRIBUTION,{id: this.itemId}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        let distributionList = Object.assign(ret.data)
                        for (let i of distributionList) {
                            i.show = false;
                            i.hide = true;
                            i.editStatus = false;
                        }
                        this.distributionList = distributionList;
                    }
                })
            },
            //
            changeInterests(index,time) {
                apiHttp(api.GET_FUND_DISTRIBUTION,{id: this.itemId}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        let distributionList = Object.assign(ret.data)
                        distributionList[index].historyList.map(item=>{
                            item.interests = '0%';
                        })
                        distributionList[index].disTime = time;
                        distributionList[index].show = false;
                        distributionList[index].hide = false;
                        distributionList[index].editStatus = true;
                        this.editData = distributionList[index];
                    }
                })
            },
            //分配总计的按钮
            distributionTotal() {
                this.dialogTotal = true;
                apiHttp(api.TOTAL_DISTRIBUTION, {fundId: this.itemId}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.totalData = ret.data;
                    }
                })
            },
            // 更改分配时间
            changeDate(val) {
                this.partnerAllList = [];
                let currentDate = new Date(val).getTime();
                let result = [];
                apiHttp(api.PARTNER_LIST,{fundId: this.itemId}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        ret.data.map(item => {
                            if (currentDate >= item.fundPartnerEntity.recordingTime) {
                                result.push(item);
                            } else {
                                result = [];
                            }
                        })
                        if (result.length) {
                            let list = result[0].distHistoryList;
                            for (let i of list) {
                                this.partnerAllList.push({
                                    lpType: i.type,
                                    lpName: i.customerName,
                                    lpId: i.customerId,
                                    attribute: '',
                                    partnerType: i.partnerType,
                                    interests: i.proportion,
                                })
                            }
                        } else {
                            this.partnerAllList = [];
                        }
                        this.distributionDate = {
                            disTime: val,
                            frequency: '',
                            fundId: this.itemId,
                            id: '',
                            historyList: this.partnerAllList,
                            itemList: [],
                            money: '',
                            reason: '',
                            fileList: [],
                        }
                        this.fileDetail = [];
                    }
                })
            },
            // 子组件传来的数据
            changeData(data, index,zero) {
                data.show = false;
                data.hide = false;
                data.editStatus = true;
                this.editData = data;
                this.editIndex = index;
                if (zero) {
                    this.$set(this.editData,'interests','0%');
                    // this.editData.interests = '0%';
                }
            },
            //获取合伙人  人的信息
            // getPartnerInfo() {
            //     let query = {
            //         name: '',    //模糊搜索的
            //         fundId: this.itemId
            //     }
            //     apiHttp(api.GET_PARTNER, query).then( ret=>{
            //         if (ret.resCode === STATUS_HTTP_SUCCESS) {
            //                 this.partnerAllList = ret.data;
            //         }
            //     })
            // },
            //跳转合伙人
            goPartnerDetail(id,type) {
                if (type === '2') { // 母基金
                    this.$router.push({name: 'invested-FOF', params: {itemId: id, tabIndex: '0', openWindow: true }});
                } else {
                    apiHttp(api.GET_GUEST, {guestId: id}).then(ret => {
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$router.push({
                                name: 'client-management-info',
                                params: {eClient_guestId: id, openWindow: true}
                            });
                        }
                    })
                }
            },
            //切换编辑
            fundDistributionEdit(row,index) {
                row.hide = false;
                this.distributionList[index].editStatus = true;
                this.editData = row;
                this.editIndex = index;
            },
            //切换展开收起
            fundAllocationOpen(index) {
                if (this.distributionList[index].show === false) {
                    this.distributionList[index].show = true;
                    this.distributionList[index].hide = false
                } else {
                    this.distributionList[index].show = false;
                    this.distributionList[index].hide = true
                }
            },
            //添加基金分配
            addDistributionData() {
                if (this.distributionDate.historyList.length) {
                    for (let i of this.distributionDate.historyList) {
                        if (!i.amount) {
                            i.amount = 0;
                        }
                    }
                    apiHttp(api.ADD_DISTRIBUTION,this.distributionDate).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '添加成功!',
                                center: true
                            });
                            this.$emit('addRedDot', 4)
                            this.dialogFormVisible = false;
                            this.getDistributionList();
                        }
                    })
                } else {
                    this.$message.warning('合伙人不能为空');
                }
            },
            //删除分配
            deleteDistribution(row) {
                this.$confirm('是否删除此条信息?', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(async () => {
                    apiHttp(api.DELETE_DISTRIBUTION,{id: row.id}).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                center: true
                            });
                            row.editStatus = false;
                            this.getDistributionList();
                        }
                    })
                });
            },
            //删除表格内的一行
            deleteDistributionSingle(list,index,indexes) {
                if (indexes === -1) {//添加时候删除一行
                    this.distributionDate.historyList.splice(index,1)
                } else {//修改时候删除一行
                    if (!list.id) {
                        this.distributionList[indexes].historyList.splice(index,1)
                    } else {
                        this.$confirm('是否删除此条合伙人信息?', '提示', {
                            cancelButtonText: '取消',
                            confirmButtonText: '确定',
                            type: 'warning'
                        }).then(async () => {
                            apiHttp(api.DELETE_SINGLE_DISTRIBUTION,{id: list.id}).then( ret=>{
                                if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                    this.$message({
                                        type: 'success',
                                        message: '删除成功!',
                                        center: true
                                    });
                                    this.getDistributionList();
                                }
                            })
                        });
                    }
                }
            },
            //关闭弹出框
            cancelDialog() {
                this.dialogFormVisible = false;
            },
            //点击加号添加一行表格
            pushDistribution(index) {
                if (index === -1) {
                    this.distributionDate.historyList.push(Object.assign({},this.historyListItem))
                } else {
                    this.distributionList[index].historyList.push(Object.assign({},this.historyListItem))
                }
            },
            //添加时候改变合伙人
            addChangePartner(index,row) {
                this.partnerAllList.map( v=>{
                    if (v.lpId === row.lpId) {
                        row.lpName = v.lpName;
                    }
                })
            },
            //取消编辑
            cancelEdit(index) {
                this.getDistributionList();
                this.distributionList[index].show = true;
                this.distributionList[index].editStatus = false;
            },
            //添加时候改变项目
            addChangeItem(val) {
                let flag = false;
                for (let i of this.distributionDate.itemList) {
                    if (i.itemId === val) {
                        flag = true;
                        return;
                    }
                }
                if (!flag) {  //true 才push
                    for (let i of this.itemListAll) {
                        if (val === i.id) {
                            this.distributionDate.itemList.push({
                                disId: '',
                                id: '',
                                itemId: val,//id
                                itemName: i.itemName,//name
                            })
                        }
                    }
                }
            },
            //添加时关闭项目
            closeItem(item) {
                this.distributionDate.itemList.splice(this.distributionDate.itemList.indexOf(item),1)
            },
            addRedDot() {
                this.$emit('addRedDot',4)
            }
        },
        components: {
            'csUpload': require('__components/upload-btn/index'),//上传文件组件
            'csDistributionEdit': require('./edit.vue')
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    table td{
        padding: 5px 5px;
        box-sizing: border-box;
    }
    .wordWrap{
        word-break: break-all;
    }
</style>
